package com.jkgl.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkgl.entity.UserInfoDayEntity;
import com.jkgl.entity.UserInfoSelfEntity;
import com.jkgl.mapper.UserInfoDayMapper;
import com.jkgl.model.result.SelfUserVO;
import com.jkgl.model.userinfoday.UserInfoDayPagination;
import com.jkgl.service.UserInfoDayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkgl.service.UserInfoSelfService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author w020304m
 * @since 2025-02-11
 */
@Service
@RequiredArgsConstructor
public class UserInfoDayServiceImpl extends ServiceImpl<UserInfoDayMapper, UserInfoDayEntity> implements UserInfoDayService {
    private final Snowflake snowflake;
    private final UserInfoSelfService selfService;

    @Override
    public List<UserInfoDayEntity> getUserInfoToday(Long userId) {
        QueryWrapper<UserInfoDayEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfoDayEntity::getUserId,userId)
                .eq(UserInfoDayEntity::getCreateTime, LocalDate.now());
        List<UserInfoDayEntity> days = new ArrayList<>();
        days.add(baseMapper.selectOne(queryWrapper));
        queryWrapper.clear();
        days.add(baseMapper.selectOne(queryWrapper.lambda().eq(UserInfoDayEntity::getUserId,userId)
                .eq(UserInfoDayEntity::getCreateTime,LocalDate.now().minusDays(1))));
        return days;
    }

    @Override
    public void create(UserInfoDayEntity userInfoDayEntity) {
//        获取当天数据
        QueryWrapper<UserInfoDayEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfoDayEntity::getCreateTime, userInfoDayEntity.getCreateTime());
        queryWrapper.lambda().eq(UserInfoDayEntity::getUserId, userInfoDayEntity.getUserId());
        UserInfoDayEntity entity = baseMapper.selectOne(queryWrapper);
//        获取昨日数据
        QueryWrapper<UserInfoDayEntity> yesterday = new QueryWrapper<>();
        yesterday.lambda().eq(UserInfoDayEntity::getUserId, userInfoDayEntity.getUserId())
                .eq(UserInfoDayEntity::getCreateTime, userInfoDayEntity.getCreateTime().minusDays(1));
        UserInfoDayEntity yesterdayEntity = baseMapper.selectOne(yesterday);
//        获取登录最多天数
        Integer maxDay = yesterdayEntity==null ? 1:yesterdayEntity.getContinuousDays()+1;
        userInfoDayEntity.setContinuousDays(maxDay);
        UserInfoSelfEntity infoSelf = selfService.getInfoSelf(userInfoDayEntity.getUserId());
        infoSelf.setMaxDays(maxDay>infoSelf.getMaxDays() ? maxDay : infoSelf.getMaxDays());
        selfService.updateById(infoSelf);
        if(entity == null){
            userInfoDayEntity.setId(snowflake.nextIdStr());
            baseMapper.insert(userInfoDayEntity);
        }else{
            userInfoDayEntity.setId(entity.getId());
            baseMapper.updateById(userInfoDayEntity);
        }
    }

    @Override
    public IPage<UserInfoDayEntity> getUserList(UserInfoDayPagination pagination) {
        QueryWrapper<UserInfoDayEntity> queryWrapper = new QueryWrapper<>();
        if(pagination.getStartTime() != null){
            queryWrapper.lambda().ge(UserInfoDayEntity::getCreateTime,pagination.getStartTime());
        }
        if(pagination.getEndTime() != null){
            queryWrapper.lambda().le(UserInfoDayEntity::getCreateTime,pagination.getEndTime());
        }
        queryWrapper.lambda().eq(UserInfoDayEntity::getUserId,pagination.getUserId())
                .orderByDesc(UserInfoDayEntity::getCreateTime);
        Page<UserInfoDayEntity> page = new Page<>(pagination.getPageNum(),pagination.getPageSize());
        return page(page, queryWrapper);
    }

    @Override
    public List<SelfUserVO> getSportGoal(Integer minAge, Integer maxAge) {
        return baseMapper.getSprotGoal(minAge,maxAge);
    }
}
